package com.wenmingliang.demo1;

import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;

public class OtherTest {

    /**
     * 获取构造器结构
     */

    @Test
    public void test1() {

        Class<Person> clazz = Person.class;

        //getConstructors()：获取当前运行时类中声明为public的构造器
        for (Constructor constructor : clazz.getConstructors()) {
            System.out.println(constructor);
        }
        System.out.println();

        // getDeclaredConstructors():获取当前运行时类中所有的构造器
        Constructor<?>[] constructors = clazz.getDeclaredConstructors();
        for(Constructor constructor : constructors) {
            System.out.println(constructor);
        }

    }

    /**
     * 获取运行时类的父类
     */
    @Test
    public void test2() {
        Class<Person> personClass = Person.class;
        Class<? super Person> superclass = personClass.getSuperclass();
        System.out.println(superclass);
    }

    /**
     * 获取运行时类的带泛型的父类
     */
    @Test
    public void test3() {
        Class personClass = Person.class;
        Type type = personClass.getGenericSuperclass();
        System.out.println(type);
    }

    /**
     * 获取运行时类实现的接口
     */
    @Test
    public void test4() {
        Class clazz = Person.class;

        Class[] interfaces = clazz.getInterfaces();
        for(Class c : interfaces) {
            System.out.println(c);
        }
    }

    /**
     * 获取运行时类所在的包
     */
    @Test
    public void test6() {
        Class<Person> clazz = Person.class;
        Package clazzPackage = clazz.getPackage();
        System.out.println(clazzPackage);
    }

    /**
     * 获取运行时类声明的注解
     */
    @Test
    public void test7() {
        Class<Person> clazz = Person.class;
        Annotation[] annotations = clazz.getAnnotations();
        for(Annotation annos : annotations) {
            System.out.println(annos);
        }

    }

    /**
     * 调用运行时类的指定属性
     */
    @Test
    public void testField() throws InstantiationException, IllegalAccessException, NoSuchFieldException {
        Class<Person> clazz = Person.class;

        //创建运行时类的对象
        Person person = clazz.newInstance();

        //获取指定的属性
        Field id = clazz.getField("id");

        //设置当前属性的值
        id.set(person, 1001);

        // 获取当前属性的值
        int o = (int) id.get(person);
        System.out.println(o);
    }

    /**
     * 如何操作运行时类的指定方法 -- 需要掌握
     */
    @Test
    public void testMethod() throws Exception {
        Class<Person> clazz = Person.class;

        //创建运行时类的对象
        Person p = clazz.newInstance();
        //1.获取指定的某个方法

        Method show = clazz.getDeclaredMethod("show", String.class);

        //保证可访问
        show.setAccessible(true);
        //调用invoke()去执行，invoke()方法的返回值即为对应类中调用方法的返回值。
        show.invoke(p,"hhh");
    }

    @Test
    public void testConstructor() throws Exception {
        Class<Person> clazz = Person.class;

        Constructor<Person> constructor = clazz.getDeclaredConstructor(String.class);
        constructor.setAccessible(true);
        Person tome = constructor.newInstance("tome");
        System.out.println(tome);
    }

}
